﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BGCOP.CAVTOP.BUSINESSLAYER;
using BGCOP.CAVTOP.COMMON;

namespace BGCOP.CAVTOP.WINFORMS
{
    public partial class frmHorario : Form
    {
        private bool agrego;

        public frmHorario()
        {
            InitializeComponent();
        }

        private void frmHorario_Load(object sender, EventArgs e)
        {
            agrego = false;
            llenarListaHorarios();
        }

        public void llenarListaHorarios()
        {
            Horarios th = new Horarios();

            listBox1.DataSource = th.ListarTodas();
            listBox1.DisplayMember = "NombreHorario";
            listBox1.ValueMember = "idHorario";

        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            agrego = true;
            txtNombreHorario.Text = "";
            chkMaestro.Checked = false;
            chkPerpetuo.Checked = false;
            FechaInicio.Value = DateTime.Now;
            fechaFin.Value = DateTime.Now;
            dataGridView1.DataSource = null;
            button1.Enabled = false;
            button2.Enabled = false;
            listBox1.SelectedIndex = -1;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            string Ddia = "";
            foreach (DataGridViewRow ro in dataGridView1.Rows)
            {
                switch(ro.Cells["dia"].Value.ToString())
                {
                    case "1":
                        Ddia = "Domingo";
                        break;
                    case "2":
                        Ddia = "Lunes";
                        break;
                    case "3":
                        Ddia = "Martes";
                        break;
                    case "4":
                        Ddia = "Miercoles";
                        break;
                    case "5":
                        Ddia = "Jueves";
                        break;
                    case "6":
                        Ddia = "Viernes";
                        break;
                    case "7":
                        Ddia = "Sábado";
                        break;
                    
                }
                ro.Cells[0].Value =Ddia;
            }
            foreach(  DataGridViewColumn col in dataGridView1.Columns)
            {
                col.Width = col.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false);
                //if(col.Name =)
                if (col.Name == "dia")
                {
                    col.Visible = false;
                }
                if (col.Name == "Observaciones")
                {
                    col.GetPreferredWidth(DataGridViewAutoSizeColumnMode.Fill, false);
                }
            }
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmHorarioDetail fm = new frmHorarioDetail();
            fm.idH = Convert.ToInt32( lblId.Text );
            fm.ShowDialog();
            DiasHorario dh = new DiasHorario();
            dataGridView1.DataSource = dh.ListarTodas(Convert.ToInt32( lblId.Text));
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                THorario th = (THorario) listBox1.SelectedItem;
                txtNombreHorario.Text = th.NombreHorario;
                if (th.esPermanente != null) chkPerpetuo.Checked = (bool) th.esPermanente;
                chkMaestro.Checked = (bool) th.esMaestro;
                chkPerpetuo.Checked = (bool) th.esPermanente;
                if (th.fechaInicio != null) FechaInicio.Value = (DateTime) th.fechaInicio;
                if (th.fechaFin != null) fechaFin.Value = (DateTime) th.fechaFin;
                lblId.Text = th.idHorario.ToString();
                DiasHorario dh = new DiasHorario();
                dataGridView1.DataSource = dh.ListarTodas(th.idHorario);
                button1.Enabled = true;
                button2.Enabled = true;
            }
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            if (agrego == true)
            {
                //nuevo registro
                Horarios hr = new Horarios();
                hr.Agregar(txtNombreHorario.Text, chkMaestro.Checked, FechaInicio.Value, fechaFin.Value, chkPerpetuo.Checked ,
                           (int) BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual);
                agrego = false;
                llenarListaHorarios();
            }
            else
            {
                //modificacion
                Horarios hr = new Horarios();
                THorario th = (THorario)listBox1.SelectedItem;
                hr.Modificar(th.idHorario ,txtNombreHorario.Text, chkMaestro.Checked, FechaInicio.Value, fechaFin.Value, chkPerpetuo.Checked,
                           (int)BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual);
                agrego = false;
                llenarListaHorarios();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
            try
            {
                DiasHorario dh = new DiasHorario();
                dh.Eliminar(Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[1].Value));
                dataGridView1.DataSource = dh.ListarTodas(Convert.ToInt32(lblId.Text));
            }
            catch (Exception)
            {
                
                
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            //eliminar horario Existente
            if (MessageBox.Show("Desea eliminar el registro seleccionado?", Properties.Settings.Default.Titulo,
                MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                Horarios hr = new Horarios();
                THorario th = (THorario) listBox1.SelectedItem;
                hr.Eliminar(th.idHorario);
                agrego = false;
                llenarListaHorarios();
            }
        }
    }
}
